package com.android.baseconfig.common.helper;

import com.android.baseconfig.common.observer.NetConnectionObserver;
import com.android.baseconfig.common.subject.NetConnectionSubject;
import com.android.baseconfig.common.utils.NetWorkUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * ---------------------------------------------------------------- <br>
 * Copyright (C) 2014-2020, by het, Shenzhen, All rights reserved.  <br>
 * ---------------------------------------------------------------- <br>
 * <p>
 * 描述: NetObserverManager <br>
 * 作者: lei <br>
 * 日期: 2017/7/25 <br>
 */
public class NetObserverManager implements NetConnectionSubject {

    private static NetObserverManager instance;
    private int currentNetType = -1;
    private List<NetConnectionObserver> observerList = new ArrayList<>();

    static {
        instance = new NetObserverManager();
    }

    public static NetObserverManager getInstance() {
        if (instance == null) {
            instance = new NetObserverManager();
        }
        return instance;
    }

    public void setCurrentNetType(int type) {
        currentNetType = type;
    }

    public int getCurrentNetType() {
        return currentNetType;
    }

    public boolean isNetConnection() {
        return currentNetType == NetWorkUtil.NET_NO_CONNECTION ? false : true;
    }

    @Override
    public void addNetObserver(NetConnectionObserver observer) {
        if (observerList != null && !observerList.contains(observer)) {
            observerList.add(observer);
        }
    }

    @Override
    public void removeNetObserver(NetConnectionObserver observer) {
        if (observerList != null && observerList.contains(observer)) {
            observerList.remove(observer);
        }
    }

    @Override
    public void notifyNetObserver(int type) {
        if (currentNetType == type) {
            return;
        }
        currentNetType = type;
        if (observerList != null && !observerList.isEmpty()) {
            for (NetConnectionObserver observer : observerList) {
                observer.updateNetStatus(currentNetType);
            }
        }
    }

    @Override
    public void notifyNFCObserver(int state) {
        if (observerList != null && !observerList.isEmpty()) {
            for (NetConnectionObserver observer : observerList) {
                observer.updateNFCStatus(state);
            }
        }
    }

    @Override
    public void notifyWifiStatusObserver(int state) {
        if (observerList != null && !observerList.isEmpty()) {
            for (NetConnectionObserver observer : observerList) {
                observer.updateWifiStatus(state);
            }
        }
    }
}
